package com.dragon.client.controller.auth;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.Log;
import com.dragon.client.config.annotation.Login;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.AdminService;
import com.dragon.logical.service.auth.DepartmentService;
import com.dragon.logical.service.auth.UploadFileService;
import com.dragon.repository.domain.dto.AdminDTO;
import com.dragon.repository.domain.vo.AdminVO;
import com.dragon.repository.entity.Admin;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "后台用户管理")
@RequestMapping("admin")
public class AdminController {

    @Autowired
    AdminService service;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    UploadFileService uploadFileService;

    @GetMapping("list")
    @ApiOperation(value = "用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search",required = false,type = "string",value = "搜索参数"),
            @ApiImplicitParam(name = "departmentId",required = false,type = "string",value = "部门Id"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页数"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数"),
    })
    public ResponseData<List<AdminDTO>> selectPage(@RequestParam(required = false)  String search,@RequestParam(required = false) Integer departmentId,@RequestParam Integer page, @RequestParam Integer limit){
        Page<Admin> adminList = service.selectPage(search,departmentId,page,limit);
        return ResponseData.success(AdminDTO.convertList(adminList.getRecords()),adminList.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加用户")
    @Log
    public ResponseData addAdmin(@RequestBody @Validated(Insert.class)AdminVO vo){
        service.add(AdminVO.convert(vo));
        return ResponseData.success();
    }

    @GetMapping("adminList")
    @ApiOperation(value = "带权限的用户列表")
    public ResponseData selectAdminList(@ApiParam(hidden = true) @Login Integer adminId){
        return ResponseData.success(AdminDTO.convertList(service.selectAdminList(adminId)));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改用户")
    @Log
    public ResponseData updateAdmin(@RequestBody @Validated(Update.class)AdminVO vo){
        service.update(AdminVO.convert(vo));
        return ResponseData.success();
    }

    @GetMapping("delete")
    @ApiOperation(value = "删除用户")
    @Log
    public ResponseData deleteAdmin(@RequestParam Integer id){
        service.delete(id);
        return ResponseData.success(true);
    }

    @GetMapping("getAdminInfo")
    @ApiOperation(value = "用户基本信息")
    public ResponseData<AdminDTO> selectAdminInfoBy(@ApiParam(hidden = true) @Login Integer id){
        Admin admin = service.getAdminInfo(id);
        return ResponseData.success(AdminDTO.convert(admin));
    }
    @GetMapping("info")
    @ApiOperation(value = "用户基本信息")
    public ResponseData<AdminDTO> selectAdminInfoById(@ApiParam(hidden = true) @Login Integer id){
        Admin admin = service.getAdminInfoById(id);
        return ResponseData.success(AdminDTO.convert(admin));
    }


    @PostMapping("deleteList")
    @ApiOperation("批量删除用户")
    @Log
    public ResponseData deleteAdminList(@ApiParam(name = "adminId", type = "array", required = true, value = "需要删除的用户id集合")  @RequestBody(required = true) List<Integer> adminIds){
        service.deleteAdminList(adminIds);
        return ResponseData.success();
    }

    @PostMapping("changeState")
    @ApiOperation("修改用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, type = "int"),
            @ApiImplicitParam(name = "state", value = "状态", required = false , type = "int")
    })
    public ResponseData changeState(@RequestParam Integer adminId, @RequestParam Integer state){
        return ResponseData.success(service.changeState(adminId, state));
    }

    @PostMapping("changePassword")
    @ApiOperation("修改用户密码")
    @Log
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", required = true, type = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, type = "String"),
            @ApiImplicitParam(name = "confirmPassword", value = "确认密码", required = true, type ="String")
    })
    public ResponseData changePassword(@ApiParam(hidden = true) @Login Integer adminId,
                                       @RequestParam String oldPassword, @RequestParam String password, @RequestParam String confirmPassword){
        service.changePassword(adminId,oldPassword,password,confirmPassword);
        return ResponseData.success();
    }

    @PostMapping("reset")
    @ApiOperation("重置用户密码")
    @Log
    public ResponseData resetPassword(
            @ApiParam(name = "ids", type = "array", required = true, value = "需要重置密码的用户id") @RequestBody List<Integer> ids){
        service.resetPassword(ids);
        return ResponseData.success();
    }

    @PostMapping("upload")
    @ApiOperation(value = "上传图片")
    @Log
    public ResponseData upload(@RequestParam("file") MultipartFile file) throws IOException {
        HashMap<String, String> url = uploadFileService.saveImage(file);
        return ResponseData.success(url);
    }

}
